/**
 * Copyright 2014 wasu.com
 *
 *
 * Create on 2015年10月21日 上午10:49:33
 */
package xt.toolkit.rpc.assemble;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

/**
 * @author <a href="mailto:caoxiaojian@wasu.cn">xiaojiancao</a>
 *
 */
@Service("rpcServiceManager")
public class RpcServiceManager {

	private Logger logger = LoggerFactory.getLogger(RpcServiceManager.class);

	private Map<String, RpcServiceDescriptor> rpcServiceDescriptors = Maps.newConcurrentMap();

	/**
	 * 检查是否已经存在同名的服务。
	 * 
	 * @param serviceName
	 * @return
	 */
	public boolean checkExistRpcService(String serviceName) {
		Preconditions.checkArgument(StringUtils.isNoneBlank(serviceName));

		return rpcServiceDescriptors.containsKey(serviceName);
	}

	public void addRpcServiceDescriptor(RpcServiceDescriptor descriptor) {
		Preconditions.checkArgument(descriptor != null && StringUtils.isNotBlank(descriptor.getBeanName()));

		String servciceName = descriptor.getServiceName();
		if (rpcServiceDescriptors.get(servciceName) != null) {
			logger.warn("Service[" + servciceName + "] exist. overwrite it.");
		}

		rpcServiceDescriptors.put(servciceName, descriptor);
	}

	/**
	 * 通过服务名称查询服务。
	 * 
	 * @param serviceName
	 * @return
	 */
	public RpcServiceDescriptor getRpcServiceByName(String serviceName) {
		Preconditions.checkArgument(StringUtils.isNotBlank(serviceName));

		return rpcServiceDescriptors.get(serviceName);
	}

}
